package com.demo.mykotlin.activity

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import com.demo.mykotlin.bean.Fruit
import com.demo.mykotlin.adapter.FruitAdapter
import com.demo.mykotlin.R
import kotlinx.android.synthetic.main.activity_list2.*

/**
 * 列表展示2
 * 自定义列表布局
 */
class List2Activity : BaseActivity() {
    private val fruitList = ArrayList<Fruit>()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_list2)

        initFruits()
        val adapter = FruitAdapter(
            this,
            R.layout.adapter_img_text,
            fruitList
        );
        listView.adapter = adapter;
        listView.setOnItemClickListener { parent, view, position, id ->
            val fruit = fruitList[position]
            Toast.makeText(this, fruit.name, Toast.LENGTH_LONG).show()
        }
    }



    private fun initFruits() {
        //重复n次函数
        repeat(2) {
            fruitList.apply {
                add(
                    Fruit(
                        "Apple",
                        R.drawable.ic_launcher_foreground
                    )
                )
                add(
                    Fruit(
                        "Banana",
                        R.drawable.ic_launcher_foreground
                    )
                )
                add(
                    Fruit(
                        "Orange",
                        R.drawable.ic_launcher_foreground
                    )
                )
                add(
                    Fruit(
                        "Watermelon",
                        R.drawable.ic_launcher_foreground
                    )
                )
                add(
                    Fruit(
                        "Pear",
                        R.drawable.ic_launcher_foreground
                    )
                )
                add(
                    Fruit(
                        "Grape",
                        R.drawable.ic_launcher_foreground
                    )
                )
                add(
                    Fruit(
                        "Pineapple",
                        R.drawable.ic_launcher_foreground
                    )
                )
                add(
                    Fruit(
                        "Cherry",
                        R.drawable.ic_launcher_foreground
                    )
                )
                add(
                    Fruit(
                        "Mango",
                        R.drawable.ic_launcher_foreground
                    )
                )
            }
        }
    }
}
